package com.shuoyue.ycgk.ui.search;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseViewHolder;
import com.shuoyue.ycgk.R;
import com.shuoyue.ycgk.base.baseadapter.AppBaseQuickAdapter;

import java.util.List;

/**
 * 搜索适配器
 */

public class HisAdapter extends AppBaseQuickAdapter<String> {

    public HisAdapter(List<String> datas) {
        super(R.layout.item_search, datas);
    }

    @Override
    protected void convert(BaseViewHolder viewHolder, String str) {
        viewHolder.addOnClickListener(R.id.tv_title);
        viewHolder.setText(R.id.tv_title, str);

    }

    /**
     * 网格布局，动态设置item的占比
     * 如果是2列，返回2，就是item占据1行。
     *
     * @param recyclerView
     */
    @Override
    public void onAttachedToRecyclerView(final RecyclerView recyclerView) {
        super.onAttachedToRecyclerView(recyclerView);

        RecyclerView.LayoutManager manager = recyclerView.getLayoutManager();
        if (manager instanceof GridLayoutManager) {
            final GridLayoutManager gridManager = ((GridLayoutManager) manager);

            gridManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
                @Override
                public int getSpanSize(int position) {
                    //注意，这里的position返回的是item在recyclerview中的位置，不是item的数据在数据列表中的位置，是把header和footer算进去的
                    String data = getItem(position);
                    if (data != null && data.length() > 9) {
                        return 2;
                    }
                    return 1;
                }
            });
        }
    }
}